package com.sto.stoapp.activity.administrator;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHhmEditText;
import com.sto.stoapp.widget.StoHhmEditText.EditClickListener;
import com.sto.stoapp.widget.StoHomeActivity;

public class ServerPortDetailActivity extends StoHomeActivity {

	private StoHhmEditText portEdit;

	private Button saveBtn;

	private int serverInfo, port;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.server_port_detail);
		serverInfo = getIntent().getIntExtra(
				AdminServerPortActivity.SERVER_INFO, -1);
		port = getIntent().getIntExtra(AdminServerPortActivity.SERVER_PORT, -1);
		this.setTitleBottom(serverInfo, HomeBroadAction.ADMINISTRATOR_BROAD);
		initWidget();
		initListener();
	}

	private void initListener() {
	    /*
	     * @修改内容： 增加小退功能
	     * @修改人：mgf
	     * @修改时间：2014-02-17
	     */
	    portEdit.setEditDelete(new EditClickListener() {
            
            @Override
            public void onEditDelete() {
                ServerPortDetailActivity.this.finish();
            }
            
            @Override
            public void onEditClickListener(View v) {
            }
        });
		saveBtn.setOnClickListener(new StoOnClickListener(this) {

			@Override
			public void mHonClick(View view) {
				String port = portEdit.getText().toString().trim();
				if (Helper.isNumeric(port)) {
					switch (serverInfo) {
					case R.string.data_port:
						StoFilePreference.getInstance(
								ServerPortDetailActivity.this)
								.setDataServicePort(Integer.valueOf(port));
						break;
					case R.string.upgrade_port:
						StoFilePreference.getInstance(
								ServerPortDetailActivity.this)
								.setUpgradeServicePort(Integer.valueOf(port));
						break;
					case R.string.order_port:
						StoFilePreference.getInstance(
								ServerPortDetailActivity.this).setOrderPort(
								Integer.valueOf(port));
						break;
					default:
						break;
					}
					Helper.showToast(ServerPortDetailActivity.this,
							getText(R.string.save_success).toString());
					finish();
				} else {
					Helper.showToast(ServerPortDetailActivity.this,
							getText(R.string.input_must_number).toString());
				}
			}
		});
	}

	private void initWidget() {
		portEdit = (StoHhmEditText) findViewById(R.id.port_edit);
		portEdit.setText("" + port);
		saveBtn = (Button) findViewById(R.id.save);
		portEdit.setSelection(portEdit.getText().toString().trim().length());
	}
}
